// internal/dao/model/mysql/customer_one_touch_video_task_info_model.go
package mysql

import (
	"context"
	"errors"
	"go-api/internal/dao/model"
	"go-api/internal/dao/schema"
	"go-api/internal/svc"
	"gorm.io/gorm"
	"time"
)

// CustomerOneTouchVideoTaskInfoModel 定义一键成片任务信息模型
type CustomerOneTouchVideoTaskInfoModel struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	db     *gorm.DB
}

// NewCustomerOneTouchVideoTaskInfoModel 创建一键成片任务信息模型实例
func NewCustomerOneTouchVideoTaskInfoModel(ctx context.Context, svcCtx *svc.ServiceContext) *CustomerOneTouchVideoTaskInfoModel {
	return &CustomerOneTouchVideoTaskInfoModel{
		ctx: ctx,
		db:  svcCtx.DbSelect.GetDb(ctx, model.DB),
	}
}

// FindInfosByStatusAndTaskIDs 根据状态和任务 ID 查找信息
func (model *CustomerOneTouchVideoTaskInfoModel) FindInfosByStatusAndTaskIDs(status int8, taskIDs []int) ([]*schema.CustomerOneTouchVideoTaskInfo, error) {
	var infos []*schema.CustomerOneTouchVideoTaskInfo
	err := model.db.Where("status = ? AND customer_one_touch_video_task_id IN ?", status, taskIDs).Limit(20).Find(&infos).Error
	return infos, err
}

// FindInfosByTaskID 根据任务 ID 查找信息
func (model *CustomerOneTouchVideoTaskInfoModel) FindInfosByTaskID(taskID int64) ([]*schema.CustomerOneTouchVideoTaskInfo, error) {
	var infos []*schema.CustomerOneTouchVideoTaskInfo
	err := model.db.Where("customer_one_touch_video_task_id = ?", taskID).Find(&infos).Error
	return infos, err
}

// UpdateInfo 更新信息
func (model *CustomerOneTouchVideoTaskInfoModel) UpdateInfo(info *schema.CustomerOneTouchVideoTaskInfo) error {
	return model.db.Save(info).Error
}

// FindOne 根据 ID 查找单个信息
func (model *CustomerOneTouchVideoTaskInfoModel) FindOne(id int64) (schema.CustomerOneTouchVideoTaskInfo, error) {
	var res schema.CustomerOneTouchVideoTaskInfo
	err := model.db.Model(&schema.CustomerOneTouchVideoTaskInfo{}).Where("customer_one_touch_video_task_id = ?", id).First(&res).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return res, nil
		}
		return res, err
	}
	return res, nil
}

// InsertSchema 插入新的信息
func (model *CustomerOneTouchVideoTaskInfoModel) InsertSchema(data *schema.CustomerOneTouchVideoTaskInfo) error {
	data.CreatedAt = time.Now()
	return model.db.Create(data).Error
}

// UpdateByMap 根据 ID 和数据映射更新信息
func (model *CustomerOneTouchVideoTaskInfoModel) UpdateByMap(id int64, data map[string]interface{}) error {
	return model.db.Model(&schema.CustomerOneTouchVideoTaskInfo{}).Where("customer_one_touch_video_task_id = ?", id).Updates(data).Error
}

// UpdateById 根据 ID 和数据映射更新信息
func (model *CustomerOneTouchVideoTaskInfoModel) UpdateById(id int64, data map[string]interface{}) error {
	return model.db.Model(&schema.CustomerOneTouchVideoTaskInfo{}).Where("id = ?", id).Updates(data).Error
}
